// pages/become-master/become-master.js
const app = getApp();
const util = require('../../utils/util.js');
let _this;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    subBoole: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    _this = this;
    _this.init();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  init:function(){
    _this.setData({
      newList: [],
      userInfo: app.globalData.xieYouUserInfo
    })
    _this.getUserDevice();
  },
  // 查询是否有设备
  getUserDevice:function(){
    let data = {};
    wx.showLoading({
      title: '正在加载...',
    })
    data.userCode = _this.data.userInfo.code;
    // 签到日期
    app.getMassage(data, null, 'getUserDevice', function (res) {
      wx.hideLoading();
      let date = res.data;
      if (res.data) {
        _this.setData({
          newList: res.data
        })
        //util.alertMsg('添加成功', 'success');
      }
    },'GET')
  },
  // 添加设备
  addDevice:function(){
    let data = {}, newList = _this.data.newList;
    if (newList.length > 0){
      if (newList[newList.length - 1].deviceBrand == '' || newList[newList.length - 1].deviceBrand == null) {
        wx.showToast({
          title: '请选择机型',
          icon: 'none'
        });
        return false;
      }
      if (newList[newList.length - 1].workHourBegin == '' || newList[newList.length - 1].workHourBegin == null) {
        wx.showToast({
          title: '请输入工时',
          icon: 'none'
        });
        return false;
      }
    }
    
    data.deviceBrand = '';
    data.deviceBrandModel = '';
    data.brandModelName = '';
    data.workHourBegin = '';
    data.add = true;
    newList.push(data);
    _this.setData({
      newList: newList,
      subBoole: true
    })
  },
  // 输入工时
  getWorkHour: function (e) {
    let workHour = e.detail.value, 
    index = e.currentTarget.dataset.index,
    newList = _this.data.newList;
    if (isNaN(workHour / 1)) {
      util.alertMsg('警告', '请输入数字', function () {
        console.log('用户点击确定')
      }, function () {
        console.log('用户点击取消')
      });
      newList[index].workHourBegin = ''
      _this.setData({
        newList: newList
      })
      return false;
    }
    newList[index].workHourBegin = workHour
    _this.setData({
      newList: newList
    })
  },
  // 提交设备信息
  deviceSubmitFun: function () {
    let list = [], newList = _this.data.newList;
    for(let i=0; i<newList.length; i++){
      let data = {};
      if (newList[i].deviceBrand == '' || newList[i].deviceBrand ==null){
        wx.showToast({
          title:'请选择机型',
          icon: 'none'
        });
        return false;
      }
      if (newList[i].workHourBegin == '' || newList[i].workHourBegin == null) {
        wx.showToast({
          title:'请输入工时',
          icon: 'none'
        });
        return false;
      }
      data.userCode = _this.data.userInfo.code;
      data.deviceBrand = newList[i].deviceBrand;
      data.deviceBrandModel = newList[i].deviceBrandModel;
      data.workHourBegin = newList[i].workHourBegin;
      list.push(data);
    }
    app.getMassage(list, null, 'deviceSubmit', function (res) {
      if (res.status == 'success'){
        util.alertMsg('提示','提交成功，正在审核中，请耐心等待',function(){
          _this.setData({
            subBoole: false
          })
          _this.init();
        },function(){
          _this.setData({
            subBoole: false
          })
          _this.init();
        })
        
      }else{
        wx.showToast({
          title: res.message,
          icon: 'none'
        });
      }
    })
  },
  // 去**页
  goNavTo: function (e) {
    app.goNavTo(e, _this.data.buttonClicked, _this);
  },
  // 返回上一页
  goBack: function (e) {
    app.goBack(e, _this.data.buttonClicked, _this);
  },
})